﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.UI;
using TaxBiz.SysConfig;
using TaxBiz.User;
using TaxEntity.User;

namespace Admin.User
{
    public partial class AddUser : BaseCommon.BasePage
    {
        protected DataTable dtDepartment = null;
        protected DataTable dtGangwei = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            string action = PageContext.GetRequest("action");
            if (!string.IsNullOrEmpty(action))
            {
                this.DoAction(action);
            }
            else
            {
                dtDepartment = new DepartmentBiz().GetList();
                this.selDepart.DataSource = dtDepartment;
                this.selDepart.DataTextField = "Name";
                this.selDepart.DataValueField = "ID";
                this.selDepart.DataBind();
                dtGangwei = PowerInfoBiz.GetGangweiList();
                this.selGangwei.DataSource = dtGangwei;
                this.selGangwei.DataTextField = "typename";
                this.selGangwei.DataValueField = "usertype";
                this.selGangwei.DataBind();
            }
        }

        private bool DoAction(string action)
        {
            UserBiz ud = new UserBiz();
            UserEntity ue = new UserEntity();
            //检查用户名
            string userName = PageContext.GetRequest("txtUserName");
            if (string.IsNullOrEmpty(userName))
            {
                Jscript.Alert("用户名不能为空");
                return false;
            }
            if (userName.Length > 20)
            {
                Jscript.Alert("用户名长度过长");
                return false;
            }
            if (ud.Exists(userName))
            {
                Jscript.Alert("用户名已存在，不能重复添加");
                return false;
            }
            ue.UserName = userName;
            //检查密码
            string password = PageContext.GetRequest("txtPassword");
            if (string.IsNullOrEmpty(password))
            {
                Jscript.Alert("密码不能为空");
                return false;
            }
            ue.Pwd = ud.GetEncryptPassword(password);
            //其它参数
            ue.SName = PageContext.GetRequest("txtRealName", 10);
            ue.DepartmentID = int.Parse(this.selDepart.Value);
            ue.UserType = int.Parse(this.selGangwei.Value);
            ue.EMail = PageContext.GetRequest("txtEmail", 50);

            ue.AddTime = DateTime.Now;
            ue.OptUserID = UserLogin.UserId;
            ue.Status = 1;
            long userID = ud.Add(ue, UserLogin);
            if (userID <= 0)
            {
                Jscript.Alert("添加失败");
                return false;
            }

            Jscript.AlertAndRedirect("添加成功", "AddUser.aspx");
            return true;
        }
    }
}